
package com.zcp.wizard;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.widget.ImageView;

import java.util.Timer;
import java.util.TimerTask;

public class GuideActivity extends Activity {
    private ImageView imageView = null;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.guide);
        // MyBroadcastReceiver.register(this);

        imageView = (ImageView) findViewById(R.id.loadImageView);
        //避免在已经安装widget情况下，自动更新时候，会停止后台服务，而widget还在造成数据错误。
        if (Util.isInstallWidget(this)) {
            Intent startIntent = new Intent(this, WidgetStatusObserverService.class);
            this.startService(startIntent);
        }
    }

    /**
     * 自动跳转下一页
     */
    private void autoGo() {
        Timer time = new Timer();
        TimerTask task = new TimerTask() {

            @Override
            public void run() {
                onClickNext();
            }

        };
        time.schedule(task, 1000 * 2);
    }

    @Override
    public void onStart() {
        startAnimation();
        autoGo();
        super.onStart();

    }

    @Override
    public void onStop() {
        stopAnimation();
        super.onStop();
    }

    public void startAnimation() {
        final AnimationDrawable frameAnimation = (AnimationDrawable) imageView.getBackground();
        Timer time = new Timer();
        TimerTask task = new TimerTask() {

            @Override
            public void run() {
                frameAnimation.start();
            }

        };
        time.schedule(task, 100);

    }

    public void stopAnimation() {
        AnimationDrawable frameAnimation = (AnimationDrawable) imageView.getBackground();
        frameAnimation.stop();
    }

    private void onClickNext() {
        boolean showTip = Util.isShowTipActivity(this);
        if (showTip) {
            Intent intent = new Intent(this, TipActivity.class);
            this.startActivity(intent);
        } else {
            Intent intent = new Intent(this, MyTabActivity.class);
            this.startActivity(intent);
        }
        this.finish();
        overridePendingTransition(R.anim.zoomin, R.anim.zoomout); // 此为自定义的动画效果，下面两个为系统的动画效果
    }

    @Override
    public void onDestroy() {
        // MyBroadcastReceiver.unUegister(this);
        imageView = null;
        super.onDestroy();
    }
}
